<?php
error_reporting(E_ALL^E_NOTICE);
$tz = ini_get('date.timezone');
if(!$tz) ini_set('date.timezone', 'Asia/Shanghai');
chdir(dirname(__FILE__));

/* this file is used to fetch data from mysql database */
class database_c{
	private $conn;
	public $debug = false;
	const CLIENT_MULTI_RESULTS=65536;/* this const property is for query by calling a procedure */
    /*fn __construct{{{*/
	function __construct(){
		$db = array(
			'Hostname' => 'localhost',
			'Database' => 'list',
			'Username' => 'root',
			'Password' => 'baggage',
			'Charset' => 'gbk',
		);
		$this->conn = mysql_connect(
			$db['Hostname'],
			$db['Username'],
			$db['Password'],
			1,
			self::CLIENT_MULTI_RESULTS
		) or die("Cant't connect host!");
		mysql_select_db($db['Database'], $this->conn)
			or die('Cant\'t select databases!');
		mysql_query('set names '.$db['Charset'], $this->conn)
			or die('Can not set charater! '.mysql_error());
    }
    /*}}}*/
    /*fn __destruct{{{*/
	function __destruct(){
		if( gettype($this->conn) == 'resource' ){
			mysql_close($this->conn);
		}
	}
    /*}}}*/

	/*fn print_sql{{{*/
	function print_sql($sql, $param){
		if(!$this->debug) return;
		elseif($this->debug==='web'){
			echo '
<div>
	<div style="border:1px solid black; padding:12px; margin:2px;float:left;">
		<span style="color:green;">'.$sql.'</span>
		<span style="color:red;">'.print_r($param, true).'</span>
	</div>
	<div style="clear:both;"></div>
</div>
				';
		}elseif($this->debug==='console'){
			echo $sql . "\n" . print_r($param, true) . "\n";
		}
	}
	/*}}}*/
	/* Bind mysql parameters */
    /*fn mysql_bind_execute{{{*/
	function mysql_bind_execute($sql,$param){
		if(empty($param)){
			$rs = mysql_query($sql,$this->conn) or die('Can not query! '.mysql_error());
			return $rs;
		}
		$this->print_sql($sql, $param);
		$sql = mysql_real_escape_string($sql);
		$sql_r = "prepare s from '".$sql."'";
		mysql_query($sql_r, $this->conn)
			or die('Mysql can not prepare: '.mysql_error());

		$temp = array();
		foreach($param as $i=>$one){
			$one = mysql_real_escape_string($one);
			$temp[0][] = "@p".$i." = '".$one."'";
			$temp[1][] = "@p".$i;
		}

		$sql_r = "set ".join($temp[0],",");
		mysql_query($sql_r, $this->conn)
			or die('Mysql can not set: '.mysql_error());

		$sql_r = "execute s using ".join($temp[1],",");
		$rs = mysql_query($sql_r, $this->conn)
			or die('Mysql can not execute: '.mysql_error());

		/*
		$sql_r = "deallocate prepare s";
		mysql_query($sql_r,$this->conn) or die('Can not deallocate! '.mysql_error());
		 */
		return $rs;
	}
    /*}}}*/

    /*fn getOne{{{*/
	function getOne($sql,$param=array(),$closeConn=false){
		$rs = $this->mysql_bind_execute($sql,$param);

		$one = mysql_fetch_array($rs,MYSQL_ASSOC);
		$one = array_values($one);

		mysql_free_result($rs);
		if($closeConn) mysql_close($this->conn);

		return $one[0];
	}
    /*}}}*/

    /*fn getRow{{{*/
	function getRow($sql,$param=array(),$closeConn=false){
		$rs = $this->mysql_bind_execute($sql,$param);
		$row = mysql_fetch_array($rs,MYSQL_ASSOC);

		mysql_free_result($rs);
		if($closeConn) mysql_close($this->conn);

		return $row;
	}
    /*}}}*/

	/* return a array contains the query results */
    /*fn getAll{{{*/
	function getAll($sql,$param=array(),$closeConn=false){
		$rs = $this->mysql_bind_execute($sql,$param);

		$dataList=array();
		while($row = mysql_fetch_array($rs, MYSQL_ASSOC)){
			$dataList[] = $row;
		}

		mysql_free_result($rs);
		if($closeConn) mysql_close($this->conn);

		return array(
			'result'=>$dataList,
			'length'=>count($dataList)
		);
	}
    /*}}}*/
	/*fn execute{{{*/
	function execute($sql, $param = array(), $closeConn = false){
		return $this->mysql_bind_execute($sql,$param);
	}
	/*}}}*/
}
$a = new database_c();
$a->debug = 'console';
$data = $a->execute('update test set val = "list" where id = ?', array(2));
print_r($data);

/* vim: set fdm=marker tabstop=4 shiftwidth=4 softtabstop=4: */
